package com.net.xpay.admin.controller.admin;

import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.xpay.admin.vo.OperationRecordVo;
import com.net.xpay.common.domain.OperationRecord;
import com.net.xpay.common.enums.config.FunctionModule;
import com.net.xpay.common.manager.OperationRecordManager;
import com.net.xpay.common.model.search.OperationRecordSearchModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 net
 * @description: 管理员操作数据记录
net
 * @create: 2020-10-21 17:20
 **/
@Slf4j
@RestController
@RequestMapping("/admin/operation-log")
@Api(tags = "运营/操作记录", description = "运营 操作记录 qyc")
public class AdminOperationLogController {

    @Autowired
    private OperationRecordManager operationRecordManager;

    @ApiOperation("搜索管理操作数据记录")
    @PostMapping("/search")
    @PreAuthorize("hasAnyRole('ADMIN', 'operation-record::search')")
    public ResultPagedList<OperationRecordVo> searchOperationRecord(@RequestBody OperationRecordSearchModel searchModel) {
        // 根据查询条件进行查询
        List<OperationRecord> records = operationRecordManager.search(searchModel);
        // 将records中数据进行处理
        List<OperationRecordVo> operationRecordVoList = new ArrayList<>(records.size());
        records.forEach(record -> {
            OperationRecordVo operationRecordVo = new OperationRecordVo();
            BeanUtils.copyProperties(record, operationRecordVo);
            operationRecordVo.setModule(record.getModule().getDesc());
            operationRecordVo.setMethodType(record.getMethodType().getDesc());
            operationRecordVoList.add(operationRecordVo);
        });
        // 查询出条数
        long count = operationRecordManager.searchCount(searchModel);
        return new ResultPagedList<>(operationRecordVoList, count, searchModel);
    }


    @GetMapping("/list-operation-record-module")
    @ApiOperation("获取所有admin用户操作的模块类别")
    @PreAuthorize("hasAnyRole('ADMIN', 'operation-record::list-operation-record-module')")
    public ResultData<List<Map<String, Object>>> listOperationRecordModule() {
        // 获取到模块类型枚举类型所有的值
        FunctionModule[] values = FunctionModule.values();
        // 进行map封装
        List<Map<String, Object>> collect = Arrays.stream(values).map(FunctionModule::toMap).collect(Collectors.toList());
        return new ResultData<>(collect);
    }


}
